package com.lanyou.esb.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.domain.FieldSystem;

@Entity
@EntityListeners({AuditingEntityListener.class})
@Table(name = "T_ESB_BU_WSDL_METADATA")
public class WsdlMetaData {

	private String id;
	
	private String wsdlAddress;
	
	private String wsdlFilePath;
	
	private FieldSystem ownerFieldSystem;
	
	private Set<WsdlMetaDataDetail> wsdlMetaDataDetail = new HashSet<WsdlMetaDataDetail>();
	
	private String isEnable;
	
	private String creator;
	
	private Date createTime;

	private String modifier;

	private Date modifyTime;
	
	
	public final static Specification<WsdlMetaData> EmptySpecification = new Specification<WsdlMetaData>(){
		@Override
		public Predicate toPredicate(Root<WsdlMetaData> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<WsdlMetaData> idSpecification(String id){
		return new Specification<WsdlMetaData>(){
			@Override
			public Predicate toPredicate(Root<WsdlMetaData> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "WSDL_METADATA_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "WSDL_URL", length = 50)
	public String getWsdlAddress() {
		return wsdlAddress;
	}

	public void setWsdlAddress(String wsdlAddress) {
		this.wsdlAddress = wsdlAddress;
	}

	@Column(name = "WSDL_FILE_PATH", length = 50)
	public String getWsdlFilePath() {
		return wsdlFilePath;
	}

	public void setWsdlFilePath(String wsdlFilePath) {
		this.wsdlFilePath = wsdlFilePath;
	}

	
	@ManyToOne
	@JoinColumn(name = "FIELD_SYSTEM_ID")
	public FieldSystem getOwnerFieldSystem() {
		return ownerFieldSystem;
	}

	public void setOwnerFieldSystem(FieldSystem ownerFieldSystem) {
		this.ownerFieldSystem = ownerFieldSystem;
	}
	
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "wsdlMetaData")
	@JsonIgnore
	public Set<WsdlMetaDataDetail> getWsdlMetaDataDetail() {
		return wsdlMetaDataDetail;
	}

	public void setWsdlMetaDataDetail(Set<WsdlMetaDataDetail> wsdlMetaDataDetail) {
		this.wsdlMetaDataDetail = wsdlMetaDataDetail;
	}


	@Column(name = "IS_ENABLE", length = 2)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}
	
	
	@CreatedBy
	@Column(name = "CREATOR", length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(name = "MODIFIER", length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	
	@Transient
	public String getFieldName() {
		try {
			return (String) getOwnerFieldSystem().getFieldName();
		} catch (Exception e) {
			return null;
		}
	}
	
	
	@Transient
	public String getFieldId() {
		try {
			return (String) getOwnerFieldSystem().getFieldId();
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getFieldSystemName() {
		try {
			return (String) getOwnerFieldSystem().getName();
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public String getFieldSystemId() {
		try {
			return (String) getOwnerFieldSystem().getId();
		} catch (Exception e) {
			return null;
		}
	}
	
}
